const app = getApp()
var API = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 100,
    receive1:[],
    receive2:[],
    send1:[],
    send2:[],
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    /** 页面配置 */
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,
    receiveQuantity:'',//收到的个数
    recevieMoney:'',//收到的金额
    sendQuantity:'',//发出的个数
    sendMoney:'',//发出的金额
    datetime: '',
    token:'',
  },  



  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  //点击tab切换 
   
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  } ,
  //事件处理函数
  // bindViewTap: function () {
  //   wx.navigateTo({
  //     url: '../logs/logs'
  //   })
  // },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    
    var _this =this

    /* 获取系统信息,给页面高度赋值 */
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });





    app.getToken().then(function (promiseRes) {
      // console.log("record.js==" + JSON.stringify(promiseRes))
      if (promiseRes.status == 200) {
        var _token = promiseRes.token;
        _this.setData({ 
          token: _token ,
          userInfo:promiseRes.userInfo
          })
        console.log("record.js==" + JSON.stringify(_this.data.userInfo))
        //网络请求：获取用户收到的红包具体信息并赋值给data里的receive
        API.recordReceive({
          success: function (res) {
            // console.log("返回的收到的红包="+JSON.stringify(res))
            if (res.data.errno == 0) {
              // console.log('res收到红包信息是：' + JSON.stringify(res));
              var arrRes = res;
              var itemReceive = res.data.data.details;
              // console.log("循环收到的红包信息为：" + JSON.stringify(itemReceive));

              // 判断列表用户的昵称是否超过6个字符，超过责多余的显示为省略号，再添加到receive1
              var arr = []
              for(var i=0;i<itemReceive.length;i++){
                var item = itemReceive[i]
                var nickName = item.nickname
                if(nickName.length > 6){
                  var newNickName = nickName.substring(0, 6) + '...'
                  item.newNickName = newNickName
                }else{
                  item.newNickName = nickName
                }

                arr.push(item)
              }

              _this.setData({
                receive1: arr,
                receive2: arrRes.data
              })

            }
          }
        })

        //网络请求：获取用户发出的红包具体信息
        API.recordSend({
          success: function (result) {
            if (result.data.errno == 0) {
              var arrResult = result;
              var itemSend = result.data.data.packets;

              _this.setData({
                send1: itemSend,
                send2: arrResult.data.data
              })
            }

          }
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: '网络连接不佳，是否尝试重新连接？',
          success: function (res) {
            if (res.confirm) {
              _this.split()
            }
            if (res.cancel) {
              wx.redirectTo({
                url: '/page/index/index',
              })
            }
          }
        })
      }
    })




    

  },  
  

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },




    // 下拉加载
  onPullDownRefresh: function (e) {
    var limit = this.data.limit + 6
    this.setData({
      limit: limit
    })
    this.requestData();
  },
})